~ chicken-core (chicken-5) /manual/Module (chicken gc)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken gc)56This module provides some control over the garbage collector.78=== gc910<procedure>(gc [FLAG])</procedure>1112Invokes a garbage-collection and returns the number of free bytes in the heap.13The flag specifies whether a minor ({{#f}}) or major ({{#t}}) GC is to be14triggered. If no argument is given, {{#t}} is assumed. An explicit {{#t}}15argument will cause all pending finalizers to be executed.1617=== current-gc-milliseconds1819<procedure>(current-gc-milliseconds)</procedure>2021Returns the number of milliseconds spent in major garbage collections since22the last call of {{current-gc-milliseconds}} and returns an exact23integer.2425=== memory-statistics2627<procedure>(memory-statistics)</procedure>2829Performs a major garbage collection and returns a three element vector30containing the total heap size in bytes, the number of bytes currently31used and the size of the nursery (the first heap generation). Note32that the actual heap is actually twice the size given in the heap size,33because CHICKEN uses a copying semi-space collector.343536=== set-finalizer!3738<procedure>(set-finalizer! X PROC)</procedure>3940Registers a procedure of one argument {{PROC}}, that will be41called as soon as the non-immediate data object {{X}} is about to42be garbage-collected (with that object as its argument).43This procedure returns {{X}}.4445Finalizers installed using {{set-finalizer!}} are invoked asynchronously,46in the thread that happens to be currently running.47Finalizers for data that has become garbage48are called on normal program exit. Finalizers are not run on49abnormal program exit. A normal program exit does not run finalizers50that are still reachable from global data.5152Multiple finalizers can be registered for the same object. The order53in which the finalizers run is undefined. Execution of finalizers54may be nested.5556NOTE 1: The finalizer will '''not''' be called while interrupts are disabled.5758NOTE 2: When a finalizable object has any weak references (i.e., weak59locatives or weak pairs) to objects that are only reachable through it60or other finalizable objects, those references will be broken like61when the objects had already been collected. This is done in order to62avoid user code from accessing objects that are possibly in an63invalid state.646566=== make-finalizer6768<procedure>(make-finalizer OBJECT ...)</procedure>6970Registers the set of non-immediate argument objects for finalization and71returns a procedure of zero or one arguments. Invoking this procedure72will return the first object from the set that73is not referenced from any other globally reachable data and can be74garbage collected.75Non-immediate objects are anything that is not a small integer ("fixnum"),76a character, a boolean, the empty list, the undefined value, the end-of-file77value ({{#!eof}}) or the broken-weak-pair object ({{#!bwp}}).7879Note that you can pass procedures created by {{make-finalizer}} to80{{make-finalizer}} itself, implying that a finalizer procedure is finalized81when all associated objects are.8283The procedure returned by {{make-finalizer}} behaves differently84depending on the argument given: If the argument is missing or {{#f}},85then it returns {{#f}} when no object has as yet been finalized.86When the argument is {{#t}}, execution of the current thread suspends until a finalization87occurs. If no other threads are executing then execution pauses for eternity.8889The same caveat regarding weak references applies to finalizers90registered with {{make-finalizer}}. See {{NOTE 2}} in {{set-finalizer!}}.9192=== add-to-finalizer9394<procedure>(add-to-finalizer FINALIZER OBJECT ...)</procedure>9596Add further objects to the finalization procedure {{FINALIZER}}, in97addition to the objects already supplied when invoking {{make-finalizer}}.9899=== force-finalizers100101<parameter>(force-finalizers)</parameter>102103If true, force and execute all pending finalizers before exiting the104program (either explicitly by {{exit}} or implicitly when the last105toplevel expression has been executed). Default is {{#t}}.106107108=== set-gc-report!109110<procedure>(set-gc-report! FLAG)</procedure>111112Print statistics after every GC, depending on {{FLAG}}. A value of113{{#t}} shows statistics after every major GC. A true value different114from {{#t}} shows statistics after every minor GC. {{#f}}115switches statistics off.116117118---119Previous: [[Module (chicken format)]]120121Next: [[Module (chicken io)]]